import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {Buffer} from "buffer";
import {ActionResponseNormal} from "./ActionResponseNormal";
import {ActionResponseNormalList} from "./ActionResponseNormalList";
import {ActionThenGetResponseNormalList} from "./ActionThenGetResponseNormalList";
import {AxdrEnum} from "../asn1.axdr/AxdrEnum";

enum Choice {
    _ERR_NONE_SELECTED = -1,
    ACTION_RESPONSE_NORMAL = 1,
    ACTION_RESPONSE_NORMAL_LIST = 2,
    ACTION_THEN_GET_RESPONSE_NORMAL_LIST = 3,
}

export class Action_Response implements AxdrType{

    dataCode : Buffer | null = null;
    choice : Choice = Choice._ERR_NONE_SELECTED;
    actionResponseNormal : ActionResponseNormal | null = null;
    actionResponseNormalList : ActionResponseNormalList | null = null;
    actionThenGetResponseNormalList : ActionThenGetResponseNormalList | null = null;

    constructor() {
    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    setActionResponse(actionResponseNormal : ActionResponseNormal) {
        this.resetChoices();
        this.choice = Choice.ACTION_RESPONSE_NORMAL;
        this.actionResponseNormal = actionResponseNormal;
    }

    setActionResponseList(actionResponseNormalList : ActionResponseNormalList){
        this.resetChoices();
        this.choice = Choice.ACTION_RESPONSE_NORMAL_LIST;
        this.actionResponseNormalList = actionResponseNormalList;
    }

    setActionThenGetResponseNormalList(actionThenGetResponseNormalList : ActionThenGetResponseNormalList){
        this.resetChoices();
        this.choice = Choice.ACTION_THEN_GET_RESPONSE_NORMAL_LIST;
        this.actionThenGetResponseNormalList = actionThenGetResponseNormalList;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;
        let choosen = new AxdrEnum();
        choosen.set_const();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = choosen.getValue();

        if (this.choice == Choice.ACTION_RESPONSE_NORMAL) {
            this.actionResponseNormal = new ActionResponseNormal();
            codeLength += this.actionResponseNormal.decode(input);
            return codeLength;
        }else if (this.choice == Choice.ACTION_RESPONSE_NORMAL_LIST) {
            this.actionResponseNormalList = new ActionResponseNormalList();
            codeLength += this.actionResponseNormalList.decode(input);
            return codeLength;
        }else if (this.choice == Choice.ACTION_THEN_GET_RESPONSE_NORMAL_LIST) {
            this.actionThenGetResponseNormalList = new ActionThenGetResponseNormalList();
            codeLength += this.actionThenGetResponseNormalList.decode(input);
            return codeLength;
        }
        throw new Error("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    encode(output: ReverseByteArrayOutputStream): number {
        if (this.dataCode != null) {
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;

        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Error("Error encoding AxdrChoice: No item in choice was selected.");
        }

        let codeLength = 0;

        if (this.choice == Choice.ACTION_THEN_GET_RESPONSE_NORMAL_LIST) {
            codeLength += this.actionThenGetResponseNormalList.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(3);
            codeLength += c.encode(output);
            return codeLength;
        }else if (this.choice == Choice.ACTION_RESPONSE_NORMAL_LIST) {
            codeLength += this.actionResponseNormalList.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(2);
            codeLength += c.encode(output);
            return codeLength;
        }else if (this.choice == Choice.ACTION_RESPONSE_NORMAL) {
            codeLength += this.actionResponseNormal.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(1);
            codeLength += c.encode(output);
            return codeLength;
        }
        throw new Error("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    getChoiceIndex(): Choice {
        return this.choice;
    }

    resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.actionResponseNormal = null;
        this.actionResponseNormalList = null;
        this.actionThenGetResponseNormalList = null
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    toString(): string {
        if (this.choice == Choice.ACTION_RESPONSE_NORMAL) {
            if (this.actionResponseNormal != null) {
                return "choice: {actionResponseNormal: " + this.actionResponseNormal + "}";
            } else {
                return "choice is actionResponseNormal but actionResponseNormal is null";
            }
        }else if(this.choice == Choice.ACTION_RESPONSE_NORMAL_LIST){
            if(this.actionResponseNormalList != null){
                return "choice: {actionResponseNormalList: " + this.actionResponseNormalList + "}";
            }else{
                return "choice is actionResponseNormalList but actionResponseNormalList is null";
            }
        }else if(this.choice == Choice.ACTION_THEN_GET_RESPONSE_NORMAL_LIST){
            if(this.actionThenGetResponseNormalList != null){
                return "choice: {actionThenGetResponseNormalList: " + this.actionThenGetResponseNormalList + "}";
            }else{
                return "choice is actionThenGetResponseNormalList but actionThenGetResponseNormalList is null";
            }
        } else {
            return "unknown";
        }
    }

}
